package com.genesis.yunnanji.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.LoginBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.fragment.PasswordLogin;
import com.google.gson.Gson;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.Map;

import es.dmoral.toasty.Toasty;



public class Setting extends BaseActivity {
    @ViewInject(R.id.rl_setting_feedback)
    private RelativeLayout rlFeedBack;
    @ViewInject(R.id.tb_setting_toolbar)
    private Toolbar mToolbar;
    @ViewInject(R.id.rl_setting_contactus)
    private RelativeLayout rlContactus;
    @ViewInject(R.id.rl_setting_logout)
    private RelativeLayout rlLogout;
    @ViewInject(R.id.rl_setting_help)
    private RelativeLayout rlHelp;
    @ViewInject(R.id.rl_setting_aboutus)
    private RelativeLayout rlAboutus;
    @ViewInject(R.id.rl_setting_account)
    private RelativeLayout rlAccount;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.activity_setting);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if (!genesisUtils.isLogin()) {
            rlLogout.setVisibility(View.GONE);
        }
    }

    @Override
    protected void setUpView() {

    }

    private Intent intent;

    @Event(value = {R.id.rl_setting_feedback, R.id.rl_setting_contactus, R.id.rl_setting_logout, R.id.rl_setting_aboutus, R.id.rl_setting_help, R.id.rl_setting_share,R.id.rl_setting_account}, type = View.OnClickListener.class)
    private void onclick(View v) {

        switch (v.getId()) {
            case R.id.rl_setting_feedback:
                intent = new Intent(context, FeedBack.class);
                startActivity(intent);
                break;
            case R.id.rl_setting_contactus:
                intent = new Intent(context, ContactUs.class);
                startActivity(intent);
                break;
            case R.id.rl_setting_logout:
                logout();
                deleteAuth(SHARE_MEDIA.QQ);
                deleteAuth(SHARE_MEDIA.WEIXIN);
                break;
            case R.id.rl_setting_aboutus:
                intent = new Intent(context, AboutUs.class);
                startActivity(intent);
                break;
            case R.id.rl_setting_help:
                intent = new Intent(context, Help.class);
                startActivity(intent);
                break;

            case R.id.rl_setting_share:
                UMImage thumb = new UMImage(this, R.mipmap.sharelog);
                UMWeb web = new UMWeb("http://beta.jinzhanji.com/Home/Index/index.html");
                web.setTitle("云南季");
                web.setThumb(thumb);
                web.setDescription("云南季");
                new ShareAction(this)
                        .withMedia(web)
                        .setDisplayList(SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SMS, SHARE_MEDIA.WEIXIN_FAVORITE, SHARE_MEDIA.EMAIL)
                        .open();
                break;
            case R.id.rl_setting_account:
                if (genesisUtils.isLogin()) {
                    intent = new Intent(context, Account.class);
                    startActivity(intent);
                } else {
                    intent = new Intent(context, Login.class);
                    startActivity(intent);
                }
                break;

        }

    }

    public void deleteAuth(SHARE_MEDIA share_media) {
        UMShareAPI.get(context).deleteOauth(this, share_media, new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {

            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {

            }
        });
    }

    public void logout() {
        RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.LOGOUT);
        params.addBodyParameter("__apptoken__", genesisUtils.getAppToken());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                LoginBean loginBean = gson.fromJson(result, LoginBean.class);
                Toasty.info(context, loginBean.getMessage(), Toast.LENGTH_SHORT).show();
                if (loginBean.getStatus() == 200) {
                    genesisUtils.setAppToken("");
                    genesisUtils.setUserImage("");
                }
                rlLogout.setVisibility(View.GONE);
                Intent intent = new Intent();
                intent.setAction(PasswordLogin.LOGIN_STATUS);
                intent.putExtra("msg", "logout");
                context.sendBroadcast(intent);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
